package controller.posterminal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;

import utility.Utility;

import model.configuration.ConfigData;
import model.posterminal.Posterminal;
import model.posterminal.PosterminalBusinessRules;
import model.posterminal.PosterminalModel;
import model.posterminal.PosterminalProducer;
import model.xml.SerializeObject;

@ManagedBean
@ViewScoped
public class PosterminalBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private DataTable posDataTable;
	private List<Posterminal> posList;					//list for view posterminal
	private List<Posterminal> filteredPos;				//list for view filtered posterminal. For component porpose
	private Posterminal	newPosterminal; 				//variable for add new posterminal
	private PosterminalProducer posterminalProducer;	//variable for return producer list
	private PosterminalModel posterminalModel;			//variable for return model list
	private List<PosterminalProducer> producerList4DDL; // list for drop down list
	private List<PosterminalModel> modelList4DDL;		// list for drop down list
	private String message4User;						// out put message for user
	private static final String dataTableFormPath = "posListView:dataTable"; // path to dataTable on form
	
	
	public PosterminalBean() throws Exception {
		posList = populatePosDataTable();	//populate datatable
		initNewPosterminal();
		initSupportVariables();
	}
	
	
	
	private List<Posterminal> populatePosDataTable() throws Exception {
		List<Posterminal> result = new ArrayList<Posterminal>();
		
		result = new Posterminal().getAllPosterminalList();
		
		return result;		
	}
	
	private DataTable getReference2DataTable() {
		 DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(dataTableFormPath);
		 return dataTable;
	}
	
	/* 
	 * wait primefaces version 3.5.7+
	public void addPosterminal() {
		RequestContext.getCurrentInstance().openDialog("addPosterminal");
	}
	*/
	
	public void addPosterminal() throws Exception{
		// 1 Add data
		newPosterminal.setPosterminalModel(posterminalModel);
		if (PosterminalBusinessRules.checkComprehensivenessData(newPosterminal)) {
			// 2 Add new posterminal to array
			posList.add(newPosterminal);
			//getReference2DataTable().loadLazyData();
			// 3 Save posterminal in database
			newPosterminal.save();
			// 4 clear field for newPosterminal
			clearFieldsOnAddForm();
			// 5 Congratulation user
			message4User = "Постерминал добавлен";
		} else {
			message4User = "Постерминал не добавлен. Заполнены не все поля или существует постерминал с аналогичными данными";
		}

		
		//RequestContext.getCurrentInstance().execute("dialogWidgetVar.hide()");
		//return null;
	}
	
	private void clearFieldsOnAddForm() throws Exception {
		initNewPosterminal();
		initSupportVariables();
	}
	
	private void initNewPosterminal() {
		newPosterminal = new Posterminal();
		newPosterminal.setInventoryNumber(ConfigData.STRINGNOVALUE);
		newPosterminal.setSerialNumber(ConfigData.STRINGNOVALUE);
	}
	
	private void initSupportVariables() throws Exception {
		int first = 0;
		posterminalProducer = new PosterminalProducer();
		posterminalModel = new PosterminalModel();
		posterminalModel.setPosterminalModelName(ConfigData.STRINGNOVALUE);
		producerList4DDL = posterminalProducer.getAllProducerList();
		
		posterminalProducer = producerList4DDL.size()>0 ? producerList4DDL.get(first) : posterminalProducer;
		
		if (producerList4DDL.size()>0) {
			// producer list exist
			modelList4DDL = posterminalModel.getPosterminalModelByProducer(posterminalProducer);
			posterminalModel = modelList4DDL.size()>0 ? modelList4DDL.get(first) : posterminalModel;
		} else {
			// producer list empty
			modelList4DDL = new ArrayList<PosterminalModel>();
		}
		
	}
	
	public void fillPossbileModelList() throws Exception {
		//this.posterminalProducer = producer;
		modelList4DDL = posterminalModel.getPosterminalModelByProducer(posterminalProducer);
	}
	
	// =================
	// geters and seters
	// =================
	
	public Posterminal getNewPosterminal() {
		return newPosterminal;
	}

	public void setNewPosterminal(Posterminal newPosterminal) {
		this.newPosterminal = newPosterminal;
	}
	
	public List<Posterminal> getPosList() {
		return posList;
	}

	public void setPosList(List<Posterminal> posList) {
		this.posList = posList;
	}

	public List<Posterminal> getFilteredPos() {
		return filteredPos;
	}

	public void setFilteredPos(List<Posterminal> filteredPos) {
		this.filteredPos = filteredPos;
	}
	
//	public List<PosterminalProducer> getProducerList() throws Exception {
//		return posterminalProducer.getAllProducerList();
//	}

	public PosterminalProducer getPosterminalProducer() {
		return posterminalProducer;
	}

	public void setPosterminalProducer(PosterminalProducer posterminalProducer) {
		this.posterminalProducer = posterminalProducer;
	}

	public PosterminalModel getPosterminalModel() {
		return posterminalModel;
	}

	public void setPosterminalModel(PosterminalModel posterminalModel) {
		this.posterminalModel = posterminalModel;
	}
	
//	public List<PosterminalModel> getPosterminalModelList() throws Exception {
//		return posterminalModel.getPosterminalModelByProducer(posterminalProducer);
//	}

	public List<PosterminalProducer> getProducerList4DDL() {
		return producerList4DDL;
	}

	public void setProducerList4DDL(List<PosterminalProducer> producerList4DDL) {
		this.producerList4DDL = producerList4DDL;
	}

	public List<PosterminalModel> getModelList4DDL() {
		return modelList4DDL;
	}

	public void setModelList4DDL(List<PosterminalModel> modelList4DDL) {
		this.modelList4DDL = modelList4DDL;
	}

	public String getMessage4User() {
		return message4User;
	}

	public void setMessage4User(String message4User) {
		this.message4User = message4User;
	}
	
	

}
